#coding:utf-8
import math
import re
from lovewith.album.views.modules.album import AlbumApi
from lovewith.utils.mtcache import MtCache
from lovewith.utils.tools import my_iterator
from lovewith.share.views.modules.attach import AttachApi
from lovewith.promote.models import PromoteHomeCycle, PromoteHomeChoice, PromoteHomeAlbum, PromoteHomeColumn


class HomeApi:
    def __init__(self):
        pass

    #首页轮播推荐
    @staticmethod
    def get_home_cycle_data():
        result = MtCache('home_cycle_new').get()

        if not result:
            #轮播图
            home_cycle = []
            cycle_promote_data = PromoteHomeCycle.objects.filter(type=4).order_by('-id')[0:5]
            for data in cycle_promote_data:
                home_cycle.append({
                    'path': data.banner.name,
                    'title': data.title,
                    'link': data.link,
                    'desc': data.description
                })
            #广告位数据
            home_ad = []
            home_ad_data = PromoteHomeCycle.objects.filter(type=5).order_by('-id')[0:3]
            for data in home_ad_data:
                home_ad.append({
                    'path': data.banner.name,
                    'link': data.link
                })

            result = {'home_cycle': home_cycle, 'home_ad': home_ad}

            MtCache('home_cycle_new').set(result)

        return result

    #首页栏目
    @staticmethod
    def get_home_column_data():
        column_data = PromoteHomeColumn.objects.all().order_by('-sort')
        return column_data

    @staticmethod
    def get_home_choice_data(is_mobile=False):
        start = 0
        end = 1
        if is_mobile:
            end = 10
        promote_data = MtCache('home_choice').get(end=end)

        if not promote_data:
            promote_data = []
            page_promote_data = PromoteHomeChoice.objects.all().order_by('-id')[start: end]
            for promote in my_iterator(page_promote_data):
                group_promote = []
                image_ids = promote.images_ids.split(',')
                for mid in image_ids:
                    image_ext_data = AttachApi().get_ext_data(mid)
                    if image_ext_data.get('image'):
                        image_data = image_ext_data.get('image')
                        colors = image_ext_data.get('colors')
                        tk_data = image_ext_data.get('tks')
                        sale_data = None
                        background = None
                        if colors:
                            background = colors[0].get('value')
                        if tk_data:
                            sale_data = tk_data[0]
                        description = re.compile(r'<[^>]+>', re.S).sub('', image_data.share_post.content)
                        group_promote.append(
                            {
                                'share_id': image_data.share_post.id,
                                'cover_id': image_data.id,
                                'cover': image_data.path,
                                'title': image_data.share_post.title,
                                'uid': image_data.share_post.user.id,
                                'name': image_data.share_post.user.username,
                                'background': background,
                                'content': description[0:400],
                                'sale_data': sale_data
                            })
                promote_data.append({
                    'title': promote.title,
                    'content': promote.content,
                    'share': group_promote
                })
            MtCache('home_choice').set(promote_data, end=end)
        return promote_data

    #推荐灵感板
    @staticmethod
    def get_home_album():
        all_album_data = MtCache('home_album').get()

        if not all_album_data:
            all_album_data = []
            album_filter_data = PromoteHomeAlbum.objects.all().order_by('-id')[0:4]
            for ab in album_filter_data:
                the_album_data = {
                    'album_id': ab.album_id,
                    'name': ab.album.name,
                    'user_id': ab.album.user.id,
                    'user_name': ab.album.user.username,
                    'user_avatar': ab.album.user.avatar,
                    'attach': []
                }
                album_filter_data = AlbumApi.get_cover(ab.album_id)
                for img in album_filter_data:
                    description = re.compile(r'<[^>]+>', re.S).sub('', img.post_image.share_post.content)
                    the_album_data['attach'].append({
                        'path': img.post_image.path,
                        'title': img.post_image.share_post.title,
                        'description': description
                    })

                all_album_data.append(the_album_data)

            MtCache('home_album').set(all_album_data)

        return all_album_data